<template>
  <div class="app-main-box">
    <!-- 历史记录表格 -->
    <Transition name="fade" mode="out-in">
      <div v-if="show_history_data == false">
        <div class="func-btn">
          <el-button round type="default" plain icon="Back" @click="$router.go(-1)">返回</el-button>
        </div>
        <div class="global-table-container" style="margin-top: 20px">
          <el-table class="global-border-radius-table" algin="center" v-loading="history_record_loading"
            :data="historyRecordList">
            <!-- 数据类型 导入数量  操作人 操作时间  -->
            <el-table-column label="序号" type="index" min-width="80" :index="indexMethod" />
            <el-table-column label="数据类型" min-width="150" prop="waybillType">
              <template #default="scope">
               <el-tag v-if="scope.row.waybillType == 'RAILWAY'" style="font-size: 14px;">铁路</el-tag>
               <el-tag v-if="scope.row.waybillType =='RECEIPT_DRAYAGE'" type="success" style="font-size: 14px;">到端</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="导入数量" min-width="150" prop="dataAmount" />
            <el-table-column label="操作人" min-width="150" prop="operatorName" />
            <el-table-column label="操作时间" min-width="150" prop="createTime" />
            <el-table-column fixed="right" label="操作" class-name="small-padding" min-width="200">
              <template #default="scope">
                <el-button link type="primary" icon="View" @click="viewHistoryData(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 页码控件 -->
        <pagination v-show="history_record_total > 0" :total="history_record_total"
          v-model:page="queryParamsRecord.pageIndex" v-model:limit="queryParamsRecord.pageSize"
          :pageSizes="[15, 20, 30, 50]" @pagination="getHistoryRecordList" />
      </div>


      <!-- 历史数据 -->
      <!-- <div v-else>
        <div class="func-btn">
          <el-button round type="default" plain icon="Back" @click="show_history_data = false">返回</el-button>
        </div>

        <div v-if="operate_data" class="operate-info">
          操作人：{{ operate_data.operatorName }}<span style="margin: 0 40px;">操作时间：{{ operate_data.createTime
            }}</span>导入数量：{{
              operate_data.dataAmount }}
        </div>

        <div class="global-table-container">
          <el-table class="global-border-radius-table" round algin="center" v-loading="loading" :data="historyData">
            <el-table-column label="磅单号" prop="weightBillNo" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="车号" prop="trainNo" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="箱号" prop="containerNo" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="篷布号" prop="coverClothNo" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="运单号" prop="waybillNo" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="货物" prop="cargo" min-width="100" :show-overflow-tooltip="true" />
            <el-table-column label="重量(吨)" prop="weight" min-width="100" :show-overflow-tooltip="true" />
            <el-table-column label="装车日期" prop="loadingDate" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="发站" prop="arrivalStation" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="到站" prop="departureStation" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="托运人" prop="consignor" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="收货人" prop="consignee" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column label="导入人" prop="creatorName" min-width="120" :show-overflow-tooltip="true" />
            <el-table-column label="导入时间" prop="createTime" min-width="200" :show-overflow-tooltip="true" />
            <el-table-column fixed="right" label="操作" class-name="small-padding" min-width="200">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="openDialog(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageIndex"
            v-model:limit="queryParams.pageSize" @pagination="getList" :pageSizes="[15, 20, 30, 50]" />
        </div>

        <el-dialog title="编辑运单" v-model="dialog_form_edit" width="800px" style="min-width: 750px" append-to-body>
          <el-form class="form-edit" :model="formEdit" ref="formEditRef" :rules="formEditRules" label-width="100px">
            <el-form-item label="磅单号：" prop="weightBillNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.weightBillNo" class="form-input" placeholder="请输入磅单号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="车号：" prop="trainNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.trainNo" class="form-input" placeholder="请输入车号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="箱号：" prop="containerNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.containerNo" class="form-input" placeholder="请输入箱号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="篷布号：" prop="coverClothNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.coverClothNo" class="form-input" placeholder="请输入篷布号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="运单号：" prop="waybillNo" class="form-items">
              <el-input v-noSpace v-model="formEdit.waybillNo" class="form-input" placeholder="请输入运单号"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="货物：" prop="cargo" class="form-items">
              <el-input v-noSpace v-model="formEdit.cargo" class="form-input" placeholder="请输入货物" clearable></el-input>
            </el-form-item>
            <el-form-item label="重量(吨)：" prop="weight" class="form-items">
              <el-input v-noSpace v-model="formEdit.weight" class="form-input" placeholder="请输入重量" clearable>
                <template #append>吨</template>
              </el-input>
            </el-form-item>
            <el-form-item label="装车日期：" prop="loadingDate" class="form-items">
              <el-date-picker value-format="YYYY-MM-DD" v-model="formEdit.loadingDate" type="date" placeholder="请选择装车日期"
                style="width: 100%;" clearable />
            </el-form-item>
            <el-form-item label="发站：" prop="arrivalStation" class="form-items">
              <el-input v-noSpace v-model="formEdit.arrivalStation" class="form-input" placeholder="请输入发站"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="到站：" prop="departureStation" class="form-items">
              <el-input v-noSpace v-model="formEdit.departureStation" class="form-input" placeholder="请输入到站"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="托运人：" prop="consignor" class="form-items">
              <el-input v-noSpace v-model="formEdit.consignor" class="form-input" placeholder="请输入托运人"
                clearable></el-input>
            </el-form-item>
            <el-form-item label="收货人：" prop="consignee" class="form-items">
              <el-input v-noSpace v-model="formEdit.consignee" class="form-input" placeholder="请输入收货人"
                clearable></el-input>
            </el-form-item>
          </el-form>

          <div class="dialog-footer" style="margin-top: 2px;">
            <el-button type="primary" @click="submitFormEdit">确 定</el-button>
            <el-button @click="cancelEdit">取 消</el-button>
          </div>
        </el-dialog>
      </div> -->
    </Transition>
  </div>


</template>

<script setup name="transportOrderList">
import { ElMessage } from 'element-plus'
import { getListApi, getHistoryListApi, updateListApi } from "@/api/transportOrderManagement/railway";
import { ref } from 'vue';
const router = useRouter();
const { proxy } = getCurrentInstance();
let show_history_data = ref(false)  // 点击历史记录列表详情展示历史数据
// let dialog_form_edit = ref(false); // 铁路运单编辑
// let loading = ref(true);
let history_record_loading = ref(false);
// let operate_data = ref(null) // 历史操作信息数据
// let total = ref(0);
let history_record_total = ref(0);
let dateRange = ref([]);
// let historyData = reactive([]) // 历史数据
let historyRecordList = ref([]) // 历史记录
/** 列表查询参数 */
const data = reactive({
  formEdit: {},
  formEditRules: {
    weightBillNo: [{ required: true, message: "磅单号不能为空", trigger: "blur" }],
    trainNo: [{ required: true, message: "车号不能为空", trigger: "blur" }],
    coverClothNo: [{ required: true, message: "篷布号不能为空", trigger: "blur" }],
    waybillNo: [{ required: true, message: "运单号不能为空", trigger: "blur" }],
    cargo: [{ required: true, message: "货物不能为空", trigger: "blur" }],
    weight: [
      { required: true, message: "重量不能为空", trigger: "blur" },
      { pattern: /^(?!0(\.0+)?$)\d+(\.\d+)?$/, message: '仅允许输入大于0的小数和整数', trigger: 'blur' },
      { pattern: /^(?!0\d)(\d+(\.\d*)?|\.\d+)$/, message: '输入内容不合法', trigger: 'blur' }
    ],
    loadingDate: [{ required: true, message: "装车日期不能为空", trigger: "blur" }],
    arrivalStation: [{ required: true, message: "发站不能为空", trigger: "blur" }],
    departureStation: [{ required: true, message: "到站不能为空", trigger: "blur" }],
    consignor: [{ required: true, message: "托运人不能为空", trigger: "blur" }],
    consignee: [{ required: true, message: "收货人不能为空", trigger: "blur" }],
    containerNo: [
      { required: true, message: "箱号不能为空", trigger: "blur" },
      // { max: 11, message: "箱号不能超过11位", trigger: "blur" }
    ]
  },
  // 历史数据
  queryParams: {
    pageIndex: 1,
    pageSize: 15,
    historyImportId: '',
    waybillType: 2 // 铁路
  },
  // 历史记录
  queryParamsRecord: {
    pageIndex: 1,
    pageSize: 15,
  }
});
const { queryParams, queryParamsRecord, formEdit, formEditRules } = toRefs(data);

/** 
* Methods
*/
/** 表格序号 */
function indexMethod(index) {
  index = index + 1
  if (queryParamsRecord.value.pageIndex > 1) {
    index = index + (queryParamsRecord.value.pageIndex - 1) * queryParamsRecord.value.pageSize
  }
  return index
}

/** 获取历史导入数据*/
function getList() {
  loading.value = true;
  let params = queryParams.value
  getListApi(proxy.addDateRange(params, dateRange.value, 'Date')).then(response => {
    if (response.code === 1) {
      historyData = response.data.records;
      total.value = Number(response.data.total);
      loading.value = false;
    } else {
      loading.value = false;
      // return false
    }
  }).catch(err => {
    loading.value = false;
    // return false
  })
}

/** 查看历史数据 */
async function viewHistoryData(row) {
  // operate_data.value = row
  // // 先清空原先的列表数据
  // historyData = []
  // show_history_data.value = true
  // queryParams.value.historyImportId = row.id
  // getList()
  router.push({
    path: '/transportOrderManagement/historyRecordInfo',
    query: {
      historyImportId: row.id,
      operatorName: row.operatorName,
      dataAmount: row.dataAmount,
      createTime: row.createTime
    }
  })
}

/** 获取历史记录 */
function getHistoryRecordList() {
  history_record_loading.value = true;
  const params = queryParamsRecord.value
  getHistoryListApi(proxy.addDateRange(params)).then(response => {
    if (response.code === 1) {
      historyRecordList.value = response.data.records;
      history_record_total.value = Number(response.data.total);
      history_record_loading.value = false;
    } else {
      history_record_loading.value = false;
    }
  }).catch(err => {
    history_record_loading.value = false;
  })
}

/** 打开编辑对话框 */
// async function openDialog(row) {
//   // 打开时重置表单
//   resetEditForm()
//   formEdit.value = { ...row }
//   dialog_form_edit.value = true;
// };

/** 提交表单 */
// function submitFormEdit() {
//   proxy.$refs["formEditRef"].validate(valid => {
//     if (valid) {
//       updateList()
//     }
//   })
// }

/** 提交编辑铁路运单列表表单 */
// function updateList() {
//   let row = { ...formEdit.value, railwayConsignmentId: formEdit.value.id }
//   updateListApi(row).then(response => {
//     if (response.code === 1) {
//       updateTableRowData(response.data)
//       dialog_form_edit.value = false
//       ElMessage({ message: '操作成功', type: 'success' })
//     } else {
//       ElMessage({ message: response.message, type: 'error' })
//     }
//   }).catch(err => {
//   })
// }

/** 手动更新表格中某行的数据 */
// function updateTableRowData(params) {
//   historyData = historyData.map(item => {
//     if (item.id.toString() == params.id.toString()) {
//       item = Object.assign(item, params)
//     }
//     return item
//   });
// }

/** 编辑运单对话框-取消按钮 */
// function cancelEdit() {
//   dialog_form_edit.value = false;
//   resetEditForm();
// }

/** 重置编辑运单对话框 */
// function resetEditForm() {
//   formEdit.value = {
//     weightBillNo: undefined,
//     trainNo: undefined,
//     containerNo: undefined,
//     coverClothNo: undefined,
//     waybillNo: undefined,
//     cargo: undefined,
//     weight: undefined,
//     loadingDate: undefined,
//     arrivalStation: undefined,
//     departureStation: undefined,
//     consignor: undefined,
//     consignee: undefined
//   }
//   proxy.resetForm("formEditRef");
// }

getHistoryRecordList()
</script>

<style lang='scss' scoped>
@import '../../../assets/styles/variables.module.scss';

.app-main-box {
  background: #fff;
  border-radius: 12px;
  min-height: calc(100vh - 125px);
  width: calc(100% - 40px);

  /** 功能按钮 */
  .func-btn {
    padding: 24px 24px 0px;
  }

  // .operate-info {
  //   text-align: center;
  //   font-size: 12px;
  //   color: $--color-info;
  //   margin: 20px 0px 14px;
  // }

  // .global-table-container {
  //   padding: 0px 24px;
  // }

  /** el-select下拉气泡框样式 */
  // .search-select-option {
  //   :deep(.el-popper) {
  //     background-color: #fff;
  //     border: none !important;
  //     box-shadow: none !important;
  //   }

  //   :deep(.el-popper__arrow) {
  //     position: absolute;
  //     left: 0;
  //     z-index: 2000;
  //     transform: translate(calc(50% - 105px), 8px) rotate(90deg);
  //   }

  //   :deep(.el-select-dropdown) {
  //     width: 210px !important;
  //     position: absolute;
  //     top: 14px;
  //     right: 10px !important; // 根据form-item padding决定，原本padding20px，后来又改成了10px
  //     background: #fff;
  //     box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  //     overflow: hidden;
  //   }
  // }
}

// .dialog-footer {
//   margin-top: 24px;
//   text-align: right;
// }</style>